package com.huawei.appgallery.netdiagnosekit.ui.widget;

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import o.azc;
import o.cmp;
import o.cmv.a;
import o.cmv.c;
import o.cmv.e;
import o.dnw;
import o.ContentManager;
import o.epp;
import o.MinimumFactory;

public class ShowLogView {
    protected static final Executor ॱ = Executors.newFixedThreadPool(1);
    private Context ˊ;
    private ListView ˋ;
    private b ˎ;
    private SimpleTextAdapter ˏ;

    static class SimpleTextAdapter extends BaseAdapter {
        private Context context;
        private int left;
        private final int right;
        private ArrayList<String> texts;
        private final int top;
        private final int topOther;

        public SimpleTextAdapter(Context context, ArrayList<String> arrayList) {
            this.context = context;
            this.texts = arrayList;
            this.left = context.getResources().getDimensionPixelOffset(a.ˊ);
            this.top = context.getResources().getDimensionPixelOffset(a.ˋ);
            this.topOther = context.getResources().getDimensionPixelOffset(a.ˏ);
            this.right = context.getResources().getDimensionPixelOffset(a.ॱ);
        }

        public int getCount() {
            return this.texts.size();
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            TextView textView;
            if (view == null) {
                view = LayoutInflater.from(this.context).inflate(e.ˋ, null);
                textView = (TextView) view.findViewById(c.ᐝॱ);
                if (azc.ˎ().ॱ() <= 17 && MinimumFactory.getBoolValue(this.context)) {
                    textView.setGravity(3);
                }
            }
            textView = (TextView) view.findViewById(c.ᐝॱ);
            if (i == 0) {
                textView.setPadding(this.left, this.top, this.right, 0);
            } else {
                textView.setPadding(this.left, this.topOther, this.right, 0);
            }
            textView.setText((CharSequence) this.texts.get(i));
            return view;
        }

        public long getItemId(int i) {
            return (long) i;
        }

        public Object getItem(int i) {
            return this.texts.get(i);
        }

        public boolean isEnabled(int i) {
            return false;
        }
    }

    public class b extends AsyncTask<File, Void, ArrayList<String>> {
        final /* synthetic */ ShowLogView ˋ;
        private ArrayList<String> ॱ = new ArrayList();

        public b(ShowLogView showLogView) {
            this.ˋ = showLogView;
        }

        protected /* synthetic */ Object doInBackground(Object[] objArr) {
            return ˊ((File[]) objArr);
        }

        protected /* synthetic */ void onPostExecute(Object obj) {
            ˊ((ArrayList) obj);
        }

        protected ArrayList<String> ˊ(File... fileArr) {
            if (fileArr == null || fileArr[0] == null || !fileArr[0].exists()) {
                return new ArrayList();
            }
            File file = fileArr[0];
            String name = file.getName();
            if (!name.equalsIgnoreCase("Diagnose.log") || file.length() > 102400) {
                return new ArrayList();
            }
            cmp.ˊ.ˋ("ReadLogTask", "read log:" + name);
            ˎ(file, this.ॱ);
            return this.ॱ;
        }

        protected void ˊ(ArrayList<String> arrayList) {
            super.onPostExecute(arrayList);
            this.ˋ.ˎ(arrayList);
        }

        private void ˎ(File file, ArrayList<String> arrayList) {
            Closeable fileInputStream;
            Closeable inputStreamReader;
            IOException e;
            Closeable closeable;
            Throwable th;
            Exception e2;
            Closeable closeable2 = null;
            try {
                fileInputStream = new FileInputStream(file);
                try {
                    inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
                } catch (IOException e3) {
                    e = e3;
                    closeable = null;
                    inputStreamReader = fileInputStream;
                    try {
                        cmp.ˊ.ˊ("ReadLogTask", "read file IOException:" + e.toString());
                        dnw.ˎ(closeable);
                        dnw.ˎ(closeable2);
                        dnw.ˎ(inputStreamReader);
                    } catch (Throwable th2) {
                        th = th2;
                        fileInputStream = inputStreamReader;
                        inputStreamReader = closeable2;
                        dnw.ˎ(closeable);
                        dnw.ˎ(inputStreamReader);
                        dnw.ˎ(fileInputStream);
                        throw th;
                    }
                } catch (Exception e4) {
                    e2 = e4;
                    closeable = null;
                    inputStreamReader = null;
                    try {
                        cmp.ˊ.ˊ("ReadLogTask", "read file Exception! " + e2.toString());
                        dnw.ˎ(closeable);
                        dnw.ˎ(inputStreamReader);
                        dnw.ˎ(fileInputStream);
                    } catch (Throwable th3) {
                        th = th3;
                        dnw.ˎ(closeable);
                        dnw.ˎ(inputStreamReader);
                        dnw.ˎ(fileInputStream);
                        throw th;
                    }
                } catch (Throwable th4) {
                    th = th4;
                    closeable = null;
                    inputStreamReader = null;
                    dnw.ˎ(closeable);
                    dnw.ˎ(inputStreamReader);
                    dnw.ˎ(fileInputStream);
                    throw th;
                }
                try {
                    closeable = new BufferedReader(inputStreamReader);
                    try {
                        StringBuilder stringBuilder = new StringBuilder();
                        while (true) {
                            String readLine = closeable.readLine();
                            if (readLine == null) {
                                dnw.ˎ(closeable);
                                dnw.ˎ(inputStreamReader);
                                dnw.ˎ(fileInputStream);
                                return;
                            } else if (readLine.trim().isEmpty()) {
                                stringBuilder.delete(stringBuilder.length() - System.lineSeparator().length(), stringBuilder.length());
                                arrayList.add(stringBuilder.toString());
                                stringBuilder.delete(0, stringBuilder.length());
                            } else {
                                stringBuilder.append(readLine);
                                stringBuilder.append(System.lineSeparator());
                            }
                        }
                    } catch (IOException e5) {
                        e = e5;
                        closeable2 = inputStreamReader;
                        inputStreamReader = fileInputStream;
                    } catch (Exception e6) {
                        e2 = e6;
                    }
                } catch (IOException e7) {
                    e = e7;
                    closeable = null;
                    closeable2 = inputStreamReader;
                    inputStreamReader = fileInputStream;
                    cmp.ˊ.ˊ("ReadLogTask", "read file IOException:" + e.toString());
                    dnw.ˎ(closeable);
                    dnw.ˎ(closeable2);
                    dnw.ˎ(inputStreamReader);
                } catch (Exception e8) {
                    e2 = e8;
                    closeable = null;
                    cmp.ˊ.ˊ("ReadLogTask", "read file Exception! " + e2.toString());
                    dnw.ˎ(closeable);
                    dnw.ˎ(inputStreamReader);
                    dnw.ˎ(fileInputStream);
                } catch (Throwable th5) {
                    th = th5;
                    closeable = null;
                    dnw.ˎ(closeable);
                    dnw.ˎ(inputStreamReader);
                    dnw.ˎ(fileInputStream);
                    throw th;
                }
            } catch (IOException e9) {
                e = e9;
                closeable = null;
                inputStreamReader = null;
                cmp.ˊ.ˊ("ReadLogTask", "read file IOException:" + e.toString());
                dnw.ˎ(closeable);
                dnw.ˎ(closeable2);
                dnw.ˎ(inputStreamReader);
            } catch (Exception e10) {
                e2 = e10;
                closeable = null;
                inputStreamReader = null;
                fileInputStream = null;
                cmp.ˊ.ˊ("ReadLogTask", "read file Exception! " + e2.toString());
                dnw.ˎ(closeable);
                dnw.ˎ(inputStreamReader);
                dnw.ˎ(fileInputStream);
            } catch (Throwable th6) {
                th = th6;
                closeable = null;
                inputStreamReader = null;
                fileInputStream = null;
                dnw.ˎ(closeable);
                dnw.ˎ(inputStreamReader);
                dnw.ˎ(fileInputStream);
                throw th;
            }
        }
    }

    public ShowLogView(Context context, ListView listView) {
        this.ˊ = context;
        this.ˋ = listView;
    }

    public void ˎ() {
        if (this.ˎ == null) {
            this.ˎ = new b(this);
            this.ˎ.executeOnExecutor(ॱ, new File[]{new File(epp.ˊ(ContentManager.getInstant().getContext()) + "Diagnose.log")});
        }
    }

    public void ˏ() {
        if (this.ˎ != null) {
            this.ˎ.cancel(true);
        }
    }

    private void ˎ(ArrayList<String> arrayList) {
        if (this.ˊ == null) {
            return;
        }
        if (this.ˏ == null || this.ˋ == null) {
            this.ˏ = new SimpleTextAdapter(this.ˊ, arrayList);
            this.ˋ.setAdapter(this.ˏ);
        }
    }
}
